﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Client_Manage : System.Web.UI.Page
{
    public int IsOpen
    {
        get { return Convert.ToInt32(ViewState["_IsOpen"]); }
        set { ViewState["_IsOpen"] = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            IsOpen = DongDong.Common.Misc.GetQueryStringAsInt("IsOpen");
            GetDrpDownValue();
        }
    }

    public void PopulateUserList()
    {

        lv_ClientList.DataSource = Client.GetAllByCondition(DongDong.Common.Misc.ConvertToInt(drpSheng.SelectedValue), DongDong.Common.Misc.ConvertToInt(drpShi.SelectedValue), DongDong.Common.Misc.ConvertToInt(drpQu.SelectedValue), DongDong.Common.Misc.ConvertToInt(drpSex.SelectedValue), DongDong.Common.Misc.ConvertToInt(drpUserType.SelectedValue), txtTelphone.Text, txtMail.Text, txtJiFenFrom.Text, txtJiFenTo.Text, txtTimeFrom.Text, txtTimeTo.Text, txtKey.Text, DongDong.Common.Misc.ConvertToInt(drpUserStatus.SelectedValue));
        lv_ClientList.DataBind();
    }

    protected void lv_ClientList_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        Client.Delete(DongDong.Common.Misc.ConvertToInt(lv_ClientList.DataKeys[e.ItemIndex].Value));
        PopulateUserList();
    }
    protected void lv_ClientList_PagePropertiesChanged(object sender, EventArgs e)
    {
        PopulateUserList();
    }
    protected void Pager_PreRender(object sender, EventArgs e)
    {
        if (lv_ClientList.Items.Count == 0)
            PopulateUserList();
    }

    public void GetDrpDownValue()
    {
        drpSheng.DataSource = S_Province.GetAll();
        drpSheng.DataValueField = "ProvinceID";
        drpSheng.DataTextField = "ProvinceName";
        drpSheng.DataBind();
        drpSheng.Items.Insert(0, new ListItem("请选择", "0"));

        drpShi.Items.Insert(0, new ListItem("请选择", "0"));
        drpQu.Items.Insert(0, new ListItem("请选择", "0"));

        drpSex.PopulateFromEnum<Client.sex>();
        drpUserType.PopulateFromEnum<Client.type>();
        drpUserType.Items.Insert(0, new ListItem("请选择", "0"));

        drpUserStatus.PopulateFromEnum<Client.userstatus>();
        drpUserStatus.Items.Insert(0, new ListItem("请选择", "0"));
    }
    protected void drpSheng_TextChanged(object sender, EventArgs e)
    {
        drpShi.DataSource = S_City.GetAllByCondition(DongDong.Common.Misc.ConvertToInt(drpSheng.SelectedValue), "");
        drpShi.DataValueField = "CityID";
        drpShi.DataTextField = "CityName";
        drpShi.DataBind();
        drpShi.Items.Insert(0, new ListItem("请选择", "0"));
        drpQu.Items.Insert(0, new ListItem("请选择", "0"));

    }
    protected void drpShi_TextChanged(object sender, EventArgs e)
    {
        drpQu.DataSource = S_District.GetAllByCondition(DongDong.Common.Misc.ConvertToInt(drpShi.SelectedValue), "");
        drpQu.DataValueField = "DistrictID";
        drpQu.DataTextField = "DistrictName";
        drpQu.DataBind();
        drpQu.Items.Insert(0, new ListItem("请选择", "0"));
    }


    public string ReturnProName(object ProID)
    {
        string ProName = "";
        //Provience objPro = Provience.Get(DongDong.Common.Misc.ConvertToInt(ProID));
        //if (objPro != null)
        //{
        //    ProName = objPro.Name;
        //}
        return ProName;
    }

    public string ReturnCityName(object CityID)
    {
        string CityName = "";
        //City objCity = City.Get(DongDong.Common.Misc.ConvertToInt(CityID));
        //if (objCity != null)
        //{
        //    CityName = objCity.Name;
        //}
        return CityName;
    }

    public string ReturnAreaName(object AreaID)
    {
        string AreaName = "";
        //Area objArea = Area.Get(DongDong.Common.Misc.ConvertToInt(AreaID));
        //if (objArea != null)
        //{
        //    AreaName = objArea.Name;
        //}
        return AreaName;
    }
    protected void lv_ClientList_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            LinkButton lbtnSelect = (LinkButton)e.Item.FindControl("lbtnSelect");
            HyperLink hplEdit = (HyperLink)e.Item.FindControl("hplEdit");
            LinkButton lbtnDelete = (LinkButton)e.Item.FindControl("lbtnDelete");
            if (IsOpen == 1)
            {
                lbtnSelect.Visible = true;
                hplEdit.Visible = false;
                lbtnDelete.Visible = false;
            }
        }
    }
    protected void lv_ClientList_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName == "SelectUser")
        {
            int KeyID = DongDong.Common.Misc.ConvertToInt(e.CommandArgument);
            Client objClient = Client.Get(KeyID);
            DongDong.Common.MessageBox.ResponseScript(this, "window.returnValue ='" + KeyID + "*" + objClient.Username + "';window.opener=null;window.open('','_self');window.close();");
        }
    }
}